package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult findAll() {
        //List<WmChannel> wmChannelList = wmChannelMapper.selectList(null);
        //为了安全着想,使用分页查询
        Page<WmChannel> page = new Page<>(1, 100);
        page = wmChannelMapper.selectPage(page, null);
        List<WmChannel> wmChannelList = page.getRecords();

        return ResponseResult.okResult(wmChannelList);
    }

    //新增
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //入参判空
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String name = wmChannel.getName();//频道名称

        //1.先查询数据库是否已经存在
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name), WmChannel::getName, name);
        Integer count = wmChannelMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称不能重复");
        }

        //2.新增频道
        wmChannel.setIsDefault(false);
        wmChannel.setCreatedTime(new Date());

        //保存到数据库
        boolean saveResult = this.save(wmChannel);
        if (!saveResult) {
            ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //入参判空,入参处理
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String name = channelDto.getName();
        Integer currentPage = channelDto.getPage();
        Integer size = channelDto.getSize();

        currentPage = currentPage == null ? 1 : currentPage;
        size = size == null ? 10 : size;

        //构建查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), WmChannel::getName, name);//根据名字模糊查询
        queryWrapper.orderByDesc(WmChannel::getOrd);//默认排序

        //构建分页查询
        Page<WmChannel> page = new Page<>(currentPage, size);

        //查询
        page = wmChannelMapper.selectPage(page, queryWrapper);
        Long total = page.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage, size, total.intValue());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    //修改
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //入参判空
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String name = wmChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }

        //频道被引用时不能禁用
        LambdaQueryWrapper<WmNews> wmNewsWrapper = new LambdaQueryWrapper<>();
        wmNewsWrapper.eq(WmNews::getStatus, 9);//已发布的
        wmNewsWrapper.eq(WmNews::getChannelId, wmChannel.getId());//被引用的
        Integer wmNewsCount = wmNewsMapper.selectCount(wmNewsWrapper);
        if (wmNewsCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称正在被已发布的文章引用,不能修改");
        }
        int updateCount = wmChannelMapper.updateById(wmChannel);

        return updateCount > 0
                ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //只有禁用的频道才能删除
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        Boolean status = wmChannel.getStatus();
        if (status) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "只有禁用的频道才能删除");
        }

        int deleteResult = wmChannelMapper.deleteById(id);

        return deleteResult > 0
                ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}